<template>
  <div class="app-container" v-enter-helper="getList">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handlePassAll">批量通过</el-button>
          <el-button type="danger" @click="handleRefuseAll">批量拒绝</el-button>
          <el-form
            :inline="true"
            :model="listQuery"
            ref="dataList2"
            class="demo-form-inline"
          >
            <el-form-item label="">
              <el-input
                type="text"
                v-model="listQuery.nickname"
                placeholder="昵称"
              ></el-input>
            </el-form-item>
            <el-form-item label="">
              <el-input
                type="text"
                v-model.number="listQuery.user_code"
                placeholder="用户编号"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchSubmit">查询</el-button>
              <el-button type="primary" @click="resetSubmit">重置</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </div>
      <div class="item">
        <el-table
          v-loading="listLoading"
          ref="dataList"
          :data="menuList"
          @selection-change="handleSelectionChange"
          style="width:100%;margin-bottom: 20px;"
          row-key="id"
          border
          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        >
          <el-table-column
            type="selection"
            :selectable="handleIsSelect"
          ></el-table-column>
          <el-table-column prop="user_code" label="用户ID"></el-table-column>
          <el-table-column prop="nickname" label="昵称"></el-table-column>
          <el-table-column prop="change_from" label="原头像">
            <template slot-scope="scope">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img :src="scope.row.change_from" alt="" class="w-64 h-64" />
                <img
                  preview="2"
                  :preview-text="`${scope.row.nickname}`"
                  slot="reference"
                  :src="scope.row.change_from"
                  alt=""
                  class="w-20"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="change_to" label="待审核头像">
            <template slot-scope="scope">
              <el-popover placement="right" max-width="420" trigger="hover">
                <img :src="scope.row.change_to" alt="" class="w-64 h-64" />
                <img
                  preview="3"
                  :preview-text="`${scope.row.nickname}`"
                  slot="reference"
                  :src="scope.row.change_to"
                  alt=""
                  class="w-20"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column prop="add_time" label="申请时间"></el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.state === 1" type="primary"
                >待审核</el-tag
              >
              <el-tag v-else-if="scope.row.state === 2" type="success"
                >审核通过</el-tag
              >
              <el-tag v-else-if="scope.row.state === 3" type="danger"
                >审核失败</el-tag
              >
              <el-tag v-else-if="scope.row.state === 4" type="success"
                >资质审核通过</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280px">
            <template slot-scope="scope">
              <el-button
                v-if="scope.row.state === 1"
                type="success"
                size="mini"
                icon="el-icon-circle-check"
                @click="handlePass(scope.row)"
                plain
                >通过
              </el-button>
              <el-button
                v-if="scope.row.state === 1"
                type="warning"
                size="mini"
                icon="el-icon-circle-check"
                @click="handleQualifyPass(scope.row)"
                plain
                >资质审核
              </el-button>
              <el-button
                v-if="scope.row.state === 1"
                type="danger"
                size="mini"
                icon="el-icon-circle-close"
                @click="handleRefuse(scope.row)"
                plain
                >拒绝
              </el-button>
              <el-tag v-else>已审核</el-tag>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.perPage"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!--  -->
    <el-dialog title="信息" :visible.sync="passVisible">
      <span style="">确定执行此操作吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="passVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmPass">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->
    <el-dialog
      title="请输入原因"
      :visible.sync="refuseVisible"
      @close="handleDialogClose('banReasonForm')"
    >
      <el-form :model="banReasonForm" ref="banReasonForm" class="demo-ruleForm">
        <el-form-item
          label=""
          prop="reason"
          :rules="[{ required: true, message: '封禁理由不能为空' }]"
        >
          <el-input type="text" v-model="banReasonForm.reason"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="refuseVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmRefuse('banReasonForm')"
            >确 定</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
    <!--  -->
    <el-dialog title="信息" :visible.sync="qualifyPassVisible">
      <span style="">确定执行此操作吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qualifyPassVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmQualifyPass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listData, postCheck } from "@/api/member/avatar";

export default {
  data() {
    return {
      confirmBtn: false,

      // 封禁原因表单
      banReasonForm: {
        reason: ""
      },
      checkWhat: "",
      qualifyPassVisible: false,
      refuseVisible: false,
      passVisible: false,
      listLoading: false,
      menuList: [],
      total: 10,
      listQuery: {
        page: 1,
        perPage: 10,
        user_code: undefined,
        nickname: undefined
      },
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkboxSelect(row, rowIndex) {
      if (row.state !== 1) {
        return false;
      } else {
        return true;
      }
    },
    /* 对于已审核过的记录， 禁止使用多选框 */
    handleIsSelect({ state }) {
      return state === 1;
    },
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      if (this.listQuery.user_code === 0 || this.listQuery.user_code) {
        urlData = urlData + `&user_code=${this.listQuery.user_code}`;
      }
      if (this.listQuery.nickname) {
        urlData = urlData + `&nickname=${this.listQuery.nickname}`;
      }
      listData(urlData).then(response => {
        this.$previewRefresh();
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false;
        this.confirmBtn = false;
      });
    },
    // 查询
    searchSubmit() {
      this.listQuery.page = 1;
      this.getList();
    },
    // 重置
    resetSubmit() {
      this.listQuery.page = 1;
      this.listQuery.perPage = 10;
      this.listQuery.nickname = undefined;
      this.listQuery.user_code = undefined;

      // this.getList()
    },

    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleQualifyPass(row) {
      this.dataIds = { ids: `${row.id}` };
      this.qualifyPassVisible = true;
    },
    confirmQualifyPass() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      let obj = {};
      obj.state = 4;
      obj.ids = this.dataIds.ids;
      postCheck(obj).then(response => {
        if (response.code === 200) {
          this.getList();
          this.qualifyPassVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 点击通过
    handlePass(row) {
      this.dataIds = { ids: `${row.id}` };
      this.passVisible = true;
    },
    // 点击批量通过
    handlePassAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选要通过的申请",
          type: "warning"
        });
        return;
      }
      this.passVisible = true;
    },
    // 点击确认通过
    confirmPass() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      if (this.multipleSelection.length !== 0) {
        let ids = "";
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`;
        }
        ids = ids.substring(0, ids.length - 1);
        this.dataIds = { ids: ids };
      }
      let obj = {};
      obj.state = 2;
      obj.ids = this.dataIds.ids;
      postCheck(obj).then(response => {
        if (response.code === 200) {
          this.getList();
          this.passVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 点击拒绝
    handleRefuse(row) {
      this.dataIds = { ids: `${row.id}` };
      this.refuseVisible = true;
    },
    // 点击批量拒绝
    handleRefuseAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: "请先勾选要拒绝的申请",
          type: "warning"
        });
        return;
      } else if (this.multipleSelection.length !== 0) {
        let ids = "";
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`;
        }
        ids = ids.substring(0, ids.length - 1);
        this.dataIds = { ids: ids };
      }
      this.refuseVisible = true;
    },
    // 点击确认拒绝
    confirmRefuse() {
      if (this.confirmBtn) return;
      this.confirmBtn = true;
      let obj = {};
      obj.state = 3;
      obj.remark = this.banReasonForm.reason;
      obj.ids = this.dataIds.ids;
      postCheck(obj).then(response => {
        if (response.code === 200) {
          this.getList();
          this.refuseVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功",
            position: "bottom-right",
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false;
        }
      });
    },
    // 监听dialog关闭时的处理事件
    handleDialogClose(formName) {
      if (this.$refs[formName]) {
        this.$refs[formName].clearValidate(); // 清除整个表单的校验
      }
    }
  }
};
</script>

<style scoped>
[v-cloak] {
  display: none;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
}

.demo-form-inline {
  float: right;
}

/deep/ .el-card__header {
  padding-bottom: 0;
}
</style>
